package year2021.month0234;

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class NioTest {

    public static void main(String[] args){
        // ByteBuffer，存储字节数据到缓冲区
        // ShortBuffer，存储字符串数据到缓冲区
        // CharBuffer，存储字符数据到缓冲区
        // IntBuffer，存储整数数据到缓冲区
        // LongBuffer，存储长整型数据到缓冲区
        // DoubleBuffer，存储小数到缓冲区
        // FloatBuffer，存储小数到缓冲区

        int capacity = 1024;
        ByteBuffer byteBuffer = ByteBuffer.allocate(capacity);
        ShortBuffer shortBuffer = ShortBuffer.allocate(capacity);
        CharBuffer charBuffer = CharBuffer.allocate(capacity);
        IntBuffer intBuffer = IntBuffer.allocate(capacity);
        LongBuffer longBuffer = LongBuffer.allocate(capacity);
        DoubleBuffer doubleBuffer = DoubleBuffer.allocate(capacity);
        FloatBuffer floatBuffer = FloatBuffer.allocate(capacity);

    }

    // java NIO read文件
    public String getCaseFileInfo(String fileName) {
        StringBuffer sb = new StringBuffer();
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(fileName));
            FileChannel fileChannel = inputStream.getChannel();
            Charset charset = Charset.forName("utf-8");
            CharsetDecoder charsetDecoder = charset.newDecoder();
            int capacity = 1024;
            ByteBuffer buffer = ByteBuffer.allocate(capacity);
            CharBuffer charBuffer = CharBuffer.allocate(capacity);
            System.out.println("buffer limit and capacity : = [" + buffer.limit() + buffer.capacity() + "]");

            int length = -1;
            try {
                while ((length = fileChannel.read(buffer)) != -1) {
                    buffer.clear();
                    charsetDecoder.decode(buffer, charBuffer, true);
                    //                    byte[] bytes = buffer.array();
                    String s = new String(charBuffer.toString());
                    sb.append(s);
                    System.out.println("s = [" + s + "]");
                }
                fileChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    // java nio write文件
    public void writeFileState(String fileName, String text) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File(fileName));
            FileChannel fileChannel = fileOutputStream.getChannel();
            ByteBuffer byteBuffer = Charset.forName("GBK").encode(text);
            int length = 0;
            try {
                while ((length = fileChannel.write(byteBuffer)) != 0) {
                    System.out.println("file length have written: " + length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    // java IO读取文件
    public static String readFile(String filePathAndName) {
        String fileContent = "";
        try {
            File f = new File(filePathAndName);
            if (f.isFile() && f.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
                    fileContent += line;
                }
                read.close();
            }
        } catch (Exception e) {
            System.out.println("读取文件内容操作出错");
            e.printStackTrace();
        }
        return fileContent;
    }

    // java IO write 文件
    public static void writeFile(String filePathAndName, String fileContent) {
        try {
            File f = new File(filePathAndName);
            if (!f.exists()) {
                f.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(fileContent + "\r\n");
            writer.close();
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }
    }

}
